package com.sinosoft.jwjw.utils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;

/**
 * Created by LB on 2018/6/6.
 */
public class RequestParamUtils {

    public HttpServletRequest request;

    public String funcid = "";
    public String requesttype = ""; //getdetail  getlist  getlistpage setdata

    public String oparams = ""; //
    public JSONObject params = new JSONObject();
    public String oauth = ""; //
    public JSONObject auth = new JSONObject();

    public Integer pageno = 1;
    public Integer pagesize = 20;


    /**
     * 解析url请求中的参数
     *
     * @param _request HttpServletRequest
     * @param _params  参数值
     */
    public RequestParamUtils(HttpServletRequest _request, String _params) {
        request = _request;
        oparams = _params;
        if (oparams == null || "".equals(oparams)) {
            oparams = "{}";
        }
        getRequestUrlData(_request);//获取url参数
        params.discard("params");
        params.discard("funcid");
        params.discard("auth");
        params = ConvertUtil.deepMerge(params, ConvertUtil.toJSONObject(oparams));//form表单params参数里的参数，覆盖url里的参数
        auth = ConvertUtil.toJSONObject(oauth);
        this.pageno = ConvertUtil.toInt(params.get("pageno"), 1);
        this.pagesize = ConvertUtil.toInt(params.get("pagesize"), pagesize);
    }


    //获取url参数
    public void getRequestUrlData(HttpServletRequest request) {
        params = new JSONObject();
        Map<String, String[]> map_url = request.getParameterMap();
        for (String key : map_url.keySet()) {
            String[] temp = (String[]) map_url.get(key);

            if ("auth".equals(key)) {
                oauth = temp[temp.length - 1];
            } else {
                params.put(key, temp[temp.length - 1]);
            }
        }
    }

    //获取formdata 的参数
    public String getRequestFormString(HttpServletRequest request) {
        //如果formData是以 &key=value的形式传输过来，否则不处理。
        //参数必须包含
        StringBuffer sb = new StringBuffer();
        InputStream inputStream;

        try {
            inputStream = request.getInputStream();
            String s;
            BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            while ((s = in.readLine()) != null) {
                sb.append(s);
            }
            in.close();
            inputStream.close();

            return sb.toString();
        } catch (Exception ee) {
            sb.append("");
            return sb.toString();
        }

    }


    /**
     * 解析url请求中的参数，并解密处理
     *
     * @param _request
     * @param _params
     *//*
    public RequestParamUtils(HttpServletRequest _request,String _params)
    {
        request = _request;
        oparams = _params;
        if("".equals(oparams)) {
        	oparams="{}";
        }else {
        	oparams = java.net.URLDecoder.decode(_params,"utf-8");   
        }
        getRequestUrlData(_request);//获取url参数
        params.discard("params");
        params.discard("funcid");
        params.discard("auth");
        params = ConverHelper.deepMerge(params,ConverHelper.toJSONObject(oparams));//form表单params参数里的参数，覆盖url里的参数
        auth = ConverHelper.toJSONObject(oauth);
        this.pageno=ConverHelper.toInt(params.get("pageno"),1);
        this.pagesize =ConverHelper.toInt(params.get("pagesize"),pagesize);
    }*/
//    public RequestParamUtils(HttpServletRequest _request, String _params, String _requesttype, String _funcid)
//    {
//        request = _request;
//        oparams = _params;
//        requesttype=_requesttype;
//        funcid = _funcid;
//
//        if("".equals(oparams)) oparams="{}";
//
//        getRequestUrlData(_request);//获取url参数
//        params.discard("params");
//        params.discard("funcid");
//        params = ConverHelper.deepMerge(params,ConverHelper.toJSONObject(oparams));//form表单params参数里的参数，覆盖url里的参数
//    }
    public String getCommonResponse5() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 5);//
        jsonObject.put("msg", "解析参数异常");//
        jsonObject.put("result", new JSONObject());//
        return jsonObject.toString();
    }
}
